package com.comdo.newup_loan.module.register;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.http.ApiService;
import com.comdo.newup_loan.module.home.ADWebviewActivity;
import com.comdo.newup_loan.util.StringUtils;
import com.comdo.newup_loan.util.TitleMenuUtil;
import com.comdo.newup_loan.widget.MyEditText;

import butterknife.BindView;

/**
 * Created by liyuan on 2017/10/30.
 */

public class RegisterActivity extends BaseActivity<RegisterPresenter> implements RegisterContract.View, View.OnClickListener {
    @BindView(R.id.ed_phone)
    MyEditText phoneEditText;
    @BindView(R.id.ed_code)
    MyEditText codeEditText;
    @BindView(R.id.ed_pass)
    MyEditText passEditText;
    @BindView(R.id.ed_pass_again)
    MyEditText passAgainEditText;
    @BindView(R.id.btn_getcode)
    Button getCodeBtn;
    @BindView(R.id.btn_submit)
    Button submitBtn;
    @BindView(R.id.checkbox)
    CheckBox checkBox;
    @BindView(R.id.tv_agreement)
    TextView agreemnetTextView;
    private int Countmun = 60;
    private Boolean isRun = true;
    private Runnable runnable;
    final Handler handler = new Handler() { // handle
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    if (Countmun == 0) {
                        isRun = false;
                        getCodeBtn.setClickable(true);
                        getCodeBtn.setText("获取验证码");
                    } else {
                        Countmun--;
                        getCodeBtn.setText(Countmun + "秒后重新获取");
                    }
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void init() {
        runnable = new Runnable() {
            @Override
            public void run() {
                if (Countmun == 0) {
                    Countmun = 60;
                    getCodeBtn.setClickable(true);
                    getCodeBtn.setText("获取动态密码");
                } else {
                    Countmun--;
                    getCodeBtn.setText(Countmun + "秒后重新发送");
                    handler.postDelayed(this, 1000);
                }

            }
        };
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_register;
    }

    @Override
    public RegisterPresenter getmPresenter() {
        return new RegisterPresenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        new TitleMenuUtil(RegisterActivity.this, "注册",false);
        getCodeBtn.setOnClickListener(this);
        submitBtn.setOnClickListener(this);
        agreemnetTextView.setOnClickListener(this);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                submitBtn.setEnabled(arg1);
            }
        });
    }

    @Override
    public void registerFail(String failMessage) {
        showToast(failMessage);
    }

    @Override
    public void register() {
    showToast("注册成功！");
        RegisterActivity.this.finish();
        toSuccessActivity();
    }

    @Override
    public void getCode() {
        getCodeBtn.setClickable(false);
        handler.postDelayed(runnable, 1000);
        showToast("发送成功");
    }

    @Override
    public void getCodeFail(String failMessage) {
        showToast(failMessage);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                if(verifySubmit())
                    mPresenter.register(phoneEditText.getText().toString(),StringUtils.Md5(passEditText.getText().toString()),codeEditText.getText().toString());
                break;
            case R.id.btn_getcode:
                if (verifyCode())
                    mPresenter.getCode(phoneEditText.getText().toString());
                break;
            case R.id.tv_agreement:
                Intent intent = new Intent(RegisterActivity.this, ADWebviewActivity.class);
                intent.putExtra("title","用户协议");
                intent.putExtra("url", ApiService.PATHS+ Config.registrationProtocol);
                startActivity(intent);
                break;
        }
    }

    private boolean verifyCode() {
        if (StringUtils.isNull(phoneEditText.getText().toString())) {
            showToast("手机号不能为空!");

            return false;
        }
        if (!StringUtils.isMobile(phoneEditText.getText().toString())) {
            showToast("手机号格式不正确!");

            return false;
        }
        return true;
    }

    private boolean verifySubmit() {
        if (StringUtils.isNull(phoneEditText.getText().toString())) {
            showToast("手机号不能为空!");
            return false;
        }
        if (StringUtils.isNull(codeEditText.getText().toString())) {
            showToast("验证码不能为空!");
            return false;
        }
        if (StringUtils.isNull(passEditText.getText().toString())) {
            showToast("密码不能为空!");
            return false;
        }
        if (StringUtils.isNull(passAgainEditText.getText().toString())) {
            showToast("确认密码不能为空!");
            return false;
        }
        if (!passAgainEditText.getText().toString()
                .equals(passEditText.getText().toString())) {
            showToast("两次密码不一致!");
            return false;
        }
        if ((passAgainEditText.length() < 6 || passAgainEditText
                .length() > 16)) {
            showToast("密码为6-16位!");
            return false;
        }
        if (!StringUtils.checkPassWordMix(passAgainEditText.getText().toString())) {
            showToast("密码必须为6到16位且必须由英文数字组合!");

            return false;
        }
        return true;
    }
    private void toSuccessActivity() {
        String userName=phoneEditText.getText().toString();
        String passWord=passEditText.getText().toString();
        Intent intent=new Intent(RegisterActivity.this,RegisterSuccessActivity.class);
        intent.putExtra("userName", userName);
        intent.putExtra("passWord", passWord);
        startActivity(intent);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }
}
